package com.itttq.kb.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.kb.model.po.TopicReplyPo;
import com.itttq.kb.model.po.UserPo;
import com.itttq.kb.model.to.ResultTo;
import com.itttq.kb.service.TopicReplyService;
import com.itttq.kb.utils.Constants;

@RestController
@RequestMapping("reply")
public class TopicReplyController extends GenericController {
	private static final long serialVersionUID = -16383467055117467L;
	@Autowired
	private TopicController topicController;
	@Autowired
	private TopicReplyService topicReplyService;
	
	@RequestMapping("reply.html")
	public ModelAndView replyTopic(TopicReplyPo topicReply, HttpServletRequest request) {
		ModelAndView mv = new  ModelAndView("redirect:/topic/detail/"+topicReply.getTopicId() + ".html");
		ResultTo rt = new ResultTo(ResultTo.FAIL);
		if(topicReply != null) {
			// 从session获取user
			UserPo user = this.getUserBySession(request);
			if(null != user) {
				topicReply.setUpdaterId(user.getId()+0l);
				topicReply.setCreatorId(user.getId()+0l);
				topicReply.setCreatorName(user.getName());
				topicReply.setCreatorAvatar(user.getAvatar());
			}
			topicReply.setNumberLove(0);
			topicReply.setNumberReply(0);
			topicReplyService.addTopicReply(topicReply);
			try {
				// 更新回复数
				topicService.updateReply(topicReply.getTopicId().intValue());
			} catch (Exception e) {
				e.printStackTrace();
			}
			rt.setCode(ResultTo.SUCCESS);
		}
		return mv;
	}

	/**
	 * 删除回复，删除后直接刷新本页（后续改为ajax异步操作）
	 * 
	 * @param id	主题ID
	 * @param tid	分类ID
	 * @param request
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("del/{id}.html")
	public ModelAndView delReply(@PathVariable("id") Integer id, int tid, HttpServletRequest request) {
		topicReplyService.delTopicReply(id);
		return topicController.getTopicDetail(tid, request);
	}
	
	/**
	 * 采纳回复，暂时有个问题，没有限制，可以采纳多个
	 * 
	 * @param id	主题ID
	 * @param tid	分类ID
	 * @param request
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("adopt/{id}.html")
	public ModelAndView adoptReply(@PathVariable("id") Integer id, Integer tid, HttpServletRequest request) {
		TopicReplyPo topicReply = new TopicReplyPo();
		topicReply.setId(id);
		topicReply.setIsAdopt(Constants.STATUTS_OPEN);
		topicReplyService.updateTopicReply(topicReply);
		return topicController.getTopicDetail(tid, request);
	}
	
	/**
	 * 点赞
	 * 
	 * @param id	主题ID
	 * @param tid	分类ID
	 * @param request
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("love/{id}.html")
	public ModelAndView love(@PathVariable("id") Integer id, Integer tid, HttpServletRequest request) {
		topicReplyService.updateNumberLover(id);
		return topicController.getTopicDetail(tid, request);
	}
}
